import Vue from "vue";
import VueRouter from "vue-router";
import iView from "iview";
import routes from "./routers";
import LayoutMain from "@/components/layout";
import store from "@/store";
Vue.use(VueRouter);
const VITE_TITLE = import.meta.env.VITE_TITLE;
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch((err) => err);
};
console.log(import.meta.env.VITE_BASE_URL);
const router = new VueRouter({
    mode: "hash",
    base: import.meta.env.VITE_BASE_URL,
    routes: [
        {
            path: "/login",
            name: "login",
            meta: {
                title: "登录",
                access: "",
                showMenu: false,
                icon: "",
            },
            component: () => import("@/views/login/login"),
        },
        {
            path: "/error/404",
            name: "error_404",
            meta: {
                title: "404",
                access: "",
                showMenu: false,
                icon: "",
            },
            component: () => import("@/views/error/404"),
        },
        {
            path: "/error/auth",
            name: "error_auth",
            meta: {
                title: "暂无权限",
                access: "",
                showMenu: false,
                icon: "",
            },
            component: () => import("@/views/error/auth"),
        },
        {
            path: "/",
            name: "_home",
            redirect: "/home",
            component: LayoutMain,
            meta: {
                title: "首页",
                access: "",
                showMenu: false,
                icon: "",
            },
            children: [
                {
                    path: "/home",
                    name: "home",
                    meta: {
                        title: "首页",
                        access: "",
                        showMenu: false,
                        icon: "el-icon-home",
                    },
                    component: () => import("@/views/home/home.vue"),
                },
            ],
        },
        ...routes,
        {
            path: "/:pathMatch(.*)",
            redirect: {
                name: "home",
            },
            name: "notMatch",
            hidden: true,
        },
    ],
});

// 鉴权
router.beforeEach((to, from, next) => {
    iView.LoadingBar.start();
    if (!store.getters.isLogin && to.name != "login") {
        next("/login");
    } else if (!to.meta.no_auth && to.meta.auth_code && !store.getters.getAuthList.includes(to.meta.auth_code)) {
        // 需要鉴权但是没有对应权限
        next("/error_auth");
    } else {
        next();
    }
});
const ignoreList = ["home", "login", "error_404", "error_auth"];
router.afterEach((to) => {
    // 修改页面title
    let title = to?.meta?.title || "";
    document.title = title ? VITE_TITLE + "-" + title : VITE_TITLE;

    // 处理进度条
    iView.LoadingBar.finish();

    //页面返回顶部
    window.scrollTo(0, 0);

    // 将路由添加到tagNavList
    // 判断是否在忽略名单中
    if (!ignoreList.includes(to.name)) {
        let tagNavList = store.getters.getTagNavList;
        let hasTag = tagNavList.find((item) => item.path == to.path);
        if (!hasTag) {
            tagNavList.push({
                path: to.path,
                name: to.name,
                meta: to?.meta || {},
                fullPath: to.fullPath,
            });
            store.dispatch("updateTagNavList", tagNavList);
        }
    }
});
export default router;
